package com.studyinsect.insect;

import org.apache.http.HttpConnection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Attributes;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

/**
 * @Author: hanll
 * @ClassName: DocumentTest
 * @Date: 2022-10-27 9:52
 * @Version: 1.0
 * @Description:
 */
public class DocumentTest {
    public static void main(String[] args) throws IOException {
        Document document = Jsoup.parse(new URL("https://www.baidu.com/"), 10000);
        Element element = document.getElementById("s_lg_img_new");
        Attributes attributes = element.attributes();
        String address = "https://" + attributes.get("src").replace("//", "");
        URL url = new URL(address);
        int size = 0;
        URLConnection httpURLConnection = url.openConnection();
        BufferedInputStream bufferedInputStream = new BufferedInputStream(httpURLConnection.getInputStream());
        FileOutputStream outputStream = new FileOutputStream("E:\\1.png");
        byte [] bytes = new byte[1024];
        while ((size = bufferedInputStream.read(bytes)) != -1) {
            outputStream.write(bytes, 0, size);
        }
        outputStream.flush();
        outputStream.close();
        bufferedInputStream.close();
    }
}
